package com.leetcode.q26;

/**
 * <p> description: 删除有序数组中的重复项
 * 双指针，一个遍历nums，一个向nums更新
 * </p>
 *
 * @author tanzc
 * @date 2022/3/30
 **/
public class RemoveDuplicates {
    public static void main(String[] args) {
        RemoveDuplicates removeDuplicates = new RemoveDuplicates();
        int[] nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int k = removeDuplicates.removeDuplicates(nums);
        for (int i = 0; i < k; i++) {
            System.out.println(nums[i]);
        }
    }

    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int temp = nums[0];
        int j = 0;
        for (int i = 0, l = nums.length; i < l; i++) {
            if (nums[i] != temp) {
                nums[++j] = nums[i];
                temp = nums[i];
            }
        }
        return j + 1;
    }
}
